import type { t_reserv_candidates } from '@/types/billdata'
import { groupBoard, groupCoils } from './groupUtils'
import { useDbStore } from '@/stores/modules/database'
import { max, sum } from './common'
export const find_cutsolution = (code: string) => {
  const dbStore = useDbStore()
  const cutsolution = dbStore.cutsolution.find((r) => r.cut_code === code)
  return cutsolution
}

export const list_coils_width_by_height = (height: number, metal: string) => {
  const dbStore = useDbStore()
  const width_group = groupCoils(
    dbStore.coils.filter((r) => {
      return r.height === height && (typeof r.metal === 'string' ? r.metal : r.metal.code) === metal
    })
  )
  return width_group
}
export const list_board_spec_by_height = (height: number, metal: string) => {
  const dbStore = useDbStore()
  const spec_group = groupBoard(
    dbStore.board.filter((r) => {
      return r.height === height && (typeof r.metal === 'string' ? r.metal : r.metal.code) === metal
    })
  )
  return spec_group
}

export const find_coils_by_width = (width: number, height: number, metal: string, min_length: number) => {
  const dbStore = useDbStore()
  const coils = dbStore.coils.filter(
    (r) =>
      r.width === width &&
      r.height === height &&
      (typeof r.metal === 'string' ? r.metal : r.metal.code) === metal &&
      r.length >= min_length
  )
  return coils
}

export const list_all_arrive_by_order = (order: string) => {
  const dbStore = useDbStore()
  return dbStore.arrive.filter((r) => r.srcbill === order)
}
